package com.gexin.controller.system.user;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.gexin.common.ExcelUtils;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.excel.AdminLogExcel;
import com.gexin.model.system.AdminLog;
import com.jfinal.ext.route.ControllerBind;
/**
 * 后台管理员模板
 * @author 肖得明
 *  新增的时候复制这个
 */
@ControllerBind(controllerKey = "/system/core/log", viewPath = "WEB-INF/system/admin/")
public class AdminLogController extends SystemBaseController {

	//首页
	public void index(){
		setAttr("log_level", getPara("log_level",null));
		render("admin_log.html");
	}
	public void query(){
		AdminLog log =  new AdminLog();
		log.setLogInfo(getPara("log_info"));
		log.setLogType(getPara("log_level"));
		String sdate = getPara("startdate");
		String edate = getPara("enddate");
		log.put("startdate", sdate);
		log.put("enddate", edate);
		renderJson(AdminLog.dao.paginate(getpage(), getpageSize(),log));
	}
	
	public void out_excel() {
		SimpleDateFormat sdf=new  SimpleDateFormat("yyyy-MM-dd");
		JSONObject object = JSONObject.parseObject(getPara("data"));
		AdminLog log =  new AdminLog();
		log.setLogInfo(object.getString("log_info"));
		log.setLogType(object.getString("log_level"));
		String sdate = object.getString("startdate");
		String edate = object.getString("enddate");
		log.put("startdate", sdate);
		log.put("enddate", edate);
		List<AdminLogExcel> list1 =new ArrayList<AdminLogExcel>();
		List<AdminLog> list2 =  AdminLog.dao.queryLogList(log);
		for (AdminLog adminLog : list2) {
			AdminLogExcel logExcel=new AdminLogExcel(adminLog.getId(),adminLog.getLogType() , adminLog.getLogInfo(),adminLog.getCreatetime());
			logExcel.setData(adminLog.getLogData());
			logExcel.setUserName(adminLog.getStr("username")+"-"+adminLog.getStr("user_relName"));
			list1.add(logExcel);
		}
		ExcelUtils.exportDocument(getResponse(), getRequest(), "日志("+sdf.format(new Date())+")", list1);
		renderNull();
	}

	
	
}


